{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2481ce50",
   "metadata": {},
   "source": [
    "# Python 变量\n",
    "变量是存储数据值的容器。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fbb6862",
   "metadata": {},
   "source": [
    "## 创建变量\n",
    "Python 没有用于声明变量的命令。\n",
    "变量在您第一次为其赋值时创建。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9d88b584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "John\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "y = \"John\"\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7336a1b",
   "metadata": {},
   "source": [
    "变量不需要用任何特定类型声明，甚至可以在设置后更改类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ff9777c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sally\n"
     ]
    }
   ],
   "source": [
    "x = 4       # x is of type int\n",
    "x = \"Sally\" # x is now of type str\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ea1d071",
   "metadata": {},
   "source": [
    "## 强制转换\n",
    "如果要指定变量的数据类型，可以通过强制转换来完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "413ca5a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = str(3)    # x will be '3'\n",
    "y = int(3)    # y will be 3\n",
    "z = float(3)  # z will be 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35b82119",
   "metadata": {},
   "source": [
    "## 获取类型\n",
    "您可以使用 type() 函数获取变量的数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c671bab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "y = \"John\"\n",
    "print(type(x))\n",
    "print(type(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc5a38ae",
   "metadata": {},
   "source": [
    "## 单引号还是双引号？\n",
    "可以使用单引号或双引号来声明字符串变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2bab6963",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = \"John\"\n",
    "# is the same as\n",
    "x = 'John'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15594741",
   "metadata": {},
   "source": [
    "## 区分大小写\n",
    "变量名区分大小写。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0397d374",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 4\n",
    "A = \"Sally\"\n",
    "#A will not overwrite a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72d6f62b",
   "metadata": {},
   "source": [
    "# 变量名"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8beccab",
   "metadata": {},
   "source": [
    "## 变量名\n",
    "变量可以有一个简短的名称（如 x 和 y）或一个更具描述性的名称（age、carname、total_volume）。 Python 变量的规则：\n",
    "\n",
    "变量名必须以字母或下划线字符开头\n",
    "\n",
    "变量名不能以数字开头\n",
    "\n",
    "变量名称只能包含字母数字字符和下划线（A-z、0-9 和 _ ）\n",
    "\n",
    "变量名区分大小写（age、Age和AGE是三个不同的变量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "77af282a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有效的变量名\n",
    "myvar = \"John\"\n",
    "my_var = \"John\"\n",
    "_my_var = \"John\"\n",
    "myVar = \"John\"\n",
    "MYVAR = \"John\"\n",
    "myvar2 = \"John\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "508adba4",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-9-2a46f2c107be>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-9-2a46f2c107be>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    2myvar = \"John\"\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 无效的变量名\n",
    "2myvar = \"John\"\n",
    "my-var = \"John\"\n",
    "my var = \"John\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc9881fe",
   "metadata": {},
   "source": [
    "## 多字变量名\n",
    "包含多个单词的变量名称可能难以阅读。\n",
    "\n",
    "您可以使用多种技术使它们更具可读性："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef04f098",
   "metadata": {},
   "source": [
    "### 骆驼形式\n",
    "除了第一个单词外，每个单词都以大写字母开头："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "170fa972",
   "metadata": {},
   "outputs": [],
   "source": [
    "myVariableName = \"John\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1421d9f",
   "metadata": {},
   "source": [
    "### 帕斯卡形式\n",
    "每个单词都以大写字母开头："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a50ae5d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "MyVariableName = \"John\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3efdef62",
   "metadata": {},
   "source": [
    "### 蛇形式\n",
    "每个单词由下划线字符分隔："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9b87042f",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_variable_name = \"John\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07396939",
   "metadata": {},
   "source": [
    "# 分配多个值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1796f88",
   "metadata": {},
   "source": [
    "## 多个变量的多个值\n",
    "Python 允许您在一行中为多个变量赋值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c8a43273",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Orange\n",
      "Banana\n",
      "Cherry\n"
     ]
    }
   ],
   "source": [
    "x, y, z = \"Orange\", \"Banana\", \"Cherry\"\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c19a0931",
   "metadata": {},
   "source": [
    "## 多个变量的一个值\n",
    "您可以在一行中为多个变量分配相同的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "70bd53d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Orange\n",
      "Orange\n",
      "Orange\n"
     ]
    }
   ],
   "source": [
    "x = y = z = \"Orange\"\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ba758df",
   "metadata": {},
   "source": [
    "## 拆分集合\n",
    "如果您在列表、元组等中有一组值。Python 允许您将值提取到变量中。 这称为拆包。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fafb27b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "x, y, z = fruits\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09bb5060",
   "metadata": {},
   "source": [
    "# 输出变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b49f3f0f",
   "metadata": {},
   "source": [
    "## 输出变量\n",
    "Python 打印语句通常用于输出变量。\n",
    "\n",
    "为了结合文本和变量，Python 使用 + 字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "483253bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is awesome\n"
     ]
    }
   ],
   "source": [
    "x = \"awesome\"\n",
    "print(\"Python is \" + x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3274e73b",
   "metadata": {},
   "source": [
    "您还可以使用 + 字符将变量添加到另一个变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "db46a63c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is awesome\n"
     ]
    }
   ],
   "source": [
    "x = \"Python is \"\n",
    "y = \"awesome\"\n",
    "z =  x + y\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abed02eb",
   "metadata": {},
   "source": [
    "对于数字，+ 字符用作数学运算符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40ff5111",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "y = 10\n",
    "print(x + y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2105194",
   "metadata": {},
   "source": [
    "如果你尝试组合一个字符串和一个数字，Python 会给你一个错误："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "92ee9b86",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-00c66e4310ff>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"John\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "x = 5\n",
    "y = \"John\"\n",
    "print(x + y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf482222",
   "metadata": {},
   "source": [
    "# 用户输入\n",
    "Python 允许用户输入。\n",
    "\n",
    "这意味着我们可以要求用户输入。\n",
    "\n",
    "Python 3.6 中的方法与 Python 2.7 中的方法略有不同。\n",
    "\n",
    "Python 3.6 使用 input() 方法。\n",
    "\n",
    "Python 2.7 使用 raw_input() 方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0e658310",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter username:aaa\n",
      "Username is: aaa\n"
     ]
    }
   ],
   "source": [
    "# Python 3.6\n",
    "username = input(\"Enter username:\")\n",
    "print(\"Username is: \" + username)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1936e067",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'raw_input' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-28144ea66f4b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Python 2.7\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0musername\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter username:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Username is: \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0musername\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'raw_input' is not defined"
     ]
    }
   ],
   "source": [
    "# Python 2.7\n",
    "username = raw_input(\"Enter username:\")\n",
    "print(\"Username is: \" + username)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efe5e5e8",
   "metadata": {},
   "source": [
    "# 全局变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fef685fb",
   "metadata": {},
   "source": [
    "## 全局变量\n",
    "在函数外部创建的变量（如上述所有示例中）被称为全局变量。\n",
    "\n",
    "每个人都可以使用全局变量，无论是在函数内部还是外部。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e311ea04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is awesome\n"
     ]
    }
   ],
   "source": [
    "x = \"awesome\"\n",
    "\n",
    "def myfunc():\n",
    "  print(\"Python is \" + x)\n",
    "\n",
    "myfunc()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60589695",
   "metadata": {},
   "source": [
    "如果在函数内部创建同名变量，该变量将是局部变量，只能在函数内部使用。 具有相同名称的全局变量将保持原样，全局且具有原始值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "93759567",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is fantastic\n",
      "Python is awesome\n"
     ]
    }
   ],
   "source": [
    "x = \"awesome\"\n",
    "\n",
    "def myfunc():\n",
    "  x = \"fantastic\"\n",
    "  print(\"Python is \" + x)\n",
    "\n",
    "myfunc()\n",
    "\n",
    "print(\"Python is \" + x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94809066",
   "metadata": {},
   "source": [
    "## 全局关键字\n",
    "通常，当您在函数内部创建变量时，该变量是局部的，并且只能在该函数内部使用。\n",
    "\n",
    "要在函数内创建全局变量，可以使用 global 关键字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2ad6228b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is fantastic\n"
     ]
    }
   ],
   "source": [
    "def myfunc():\n",
    "  global x\n",
    "  x = \"fantastic\"\n",
    "\n",
    "myfunc()\n",
    "\n",
    "print(\"Python is \" + x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c54abf94",
   "metadata": {},
   "source": [
    "此外，如果要更改函数内的全局变量，请使用 global 关键字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4ec5da48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python is fantastic\n"
     ]
    }
   ],
   "source": [
    "x = \"awesome\"\n",
    "\n",
    "def myfunc():\n",
    "  global x\n",
    "  x = \"fantastic\"\n",
    "\n",
    "myfunc()\n",
    "\n",
    "print(\"Python is \" + x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
